import EngineContext from "../EngineContext"
import dataTypeEnum from "../enum/dataType.enum"
import storageTypeEnum from "../enum/storageType.enum"
import MarketContext from "../MarketContext"
import Module from "../module/Module"
import { CollectionSync } from "../storage/DataSync"
import DataSyncStore from "../storage/DataSyncStore"
import Store from "../storage/Store"
import Logger from "../utils/Logger"
import extobjUtil from "./extobj.util"
import ExtObjData from "./ExtObjData"
const log = new Logger("extobj-data")
export default class ExtObjDataModule extends Module {
  engine: EngineContext
  market: MarketContext
  store: DataSyncStore<any>
  extData: ExtObjData
  constructor() {
    super(...arguments)
    this.name = "extobj-data"
  }
  async init(e: any, t: EngineContext) {
    const { readonly } = e
    this.engine = t
    this.market = this.engine.market
    this.store = this.getStore(e)
    const data = (await this.store.read()) || {}
    this.extData = new ExtObjData(data)
    this.market.register(this, ExtObjData, this.extData)
  }
  getStore(e: any) {
    const { modelId, baseUrl, queue } = e
    log.info(`Loading ExtObj from ${storageTypeEnum.JSONSTORE}`)
    // let path = `${baseUrl}/scene-portal/${modelId}/models/sweeps?tag=showcase`
    let path = `./json/extobj.json`
    const ReadOnlyStore = new Store.ReadOnlyStore({
      queue,
      path,
      deserialize: extobjUtil.deserialize,
      cachedData: { data: null }
    })
    const Sync = new CollectionSync({ publishStores: [ReadOnlyStore] })
    return new DataSyncStore(Sync, {
      dataType: dataTypeEnum.EXTOBJ,
      engine: this.engine
    })
  }
}
